package com.flight.trans.http.network;

import android.text.TextUtils;

import com.flight.trans.activity.LoginActivity;
import com.flight.trans.bean.CommonBean;
import com.flight.trans.global.Constant;
import com.flight.trans.global.MainApplication;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.ProgressUtils;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * @author : YanKing
 * @date : 2019/6/11  15:38
 * @Description : 网络回调
 */
public abstract class OkGoCallBackListener extends StringCallback {

    private OkGoBackListener listener;
    private boolean dismissSingleDialog ;

    public OkGoCallBackListener(OkGoBackListener listener) {
        this.listener = listener;
    }

    @Override
    public void onSuccess(Response<String> response) {
        ProgressUtils.dismissProgress();

        if(dismissSingleDialog)
            ProgressUtils.dismissSingleProgress();

        if (response != null && !TextUtils.isEmpty(response.body())) {
            try {
                CommonBean commonBean = JsonUtils.parseJsonToBean(response.body(), CommonBean.class);

                if(commonBean.getCode() == Constant.CODE_SUCCESS){
                    // 请求成功
                    onSuccess(response.body());
                    CommonUtils.writeCrashLog("网络请求成功");
                }else if(commonBean.getCode() == Constant.LOGIN_OUT){
                    CommonUtils.writeCrashLog("登出");
                    listener.onError();
                    listener.getActivity().turnToAct(LoginActivity.class);
                    MainApplication.finishActivity();
                } else {
                    // 请求失败
                    listener.onWrong(commonBean.getMsg());
                    CommonUtils.writeCrashLog("网络请求失败:" + commonBean.getCode() + "..." + commonBean.getMsg());
                    listener.onError();
                }
            } catch (Exception e) {
                e.printStackTrace();
                listener.onChangeError(e.getMessage());
                CommonUtils.writeCrashLog("网络请求异常:" + e.getMessage());
                listener.onError();
            }
        } else {
            CommonUtils.writeCrashLog("网络请求异常:返回结果为空");
            listener.onDataEmpty();
            listener.onError();
        }
    }

    public OkGoCallBackListener(OkGoBackListener listener , boolean dismissSingleDialog) {
        this.listener = listener;
        this.dismissSingleDialog = dismissSingleDialog ;
    }

    @Override
    public void onError(Response<String> response) {
        super.onError(response);
        CommonUtils.writeCrashLog("网络请求失败1:" + response.code() + "..." + response.message() + "...." + response.toString());
        ProgressUtils.dismissProgress();
        ProgressUtils.dismissSingleProgress();
        listener.onError(response.message());
        listener.onError();
        CommonUtils.writeCrashLog("网络请求失败1:" + response.code() + "..." + response.message() + "...." + response.toString());
    }

    @Override
    public void onFinish() {
        CommonUtils.writeCrashLog("网络请求结束");
    }

    public abstract void onSuccess(String body);
}
